﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TrabiBT
{
    public class Variable
    {
        private object currentValue;
        private byte size;
        private int address;
        private string[] addrParts = new string[4];
        private string[] dataParts = new string[8];
        private Queue<object> rxData = new Queue<object>();
        private string type;
        private List<IView> views = new List<IView>();

        public Variable(int addr, int data, string type)
        {
            string addrString;
            string tempDataStr;

            this.type = type;
            this.address = addr;

            /* Convert address to hexadecimal string */
            addrString = addr.ToString("X");
            
            /* Store hexadecimal string in a byte array (ready to send format)*/
            for (int i = 0; i < 4; i++)
            {
               addrParts[3 - i] = addrString.Substring(i*2,2);
            }

            /* Store data-to-send string*/
            tempDataStr = data.ToString("X");
            this.size = Convert.ToByte(tempDataStr.Length / 2);
            for (int i = 0; i < this.size; i++)
            {
                dataParts[i] = tempDataStr.Substring(i * 2, 2);
            }
        }
        public object CurrentValue
        {
            set { currentValue = value; }
            get { return currentValue; }
        }
        public byte Size
        {
            set { size = value; }
            get { return size; }
        }

        public int Addr
        {
            set { address = value  ;}
            get { return address; }
        }

        public string[] AddrParts
        {
            get { return addrParts; }
        }

        public string[] DataParts
        {
            get { return dataParts; }
        }
        
        public Queue<object> RxData 
        {
            set{ rxData.Enqueue(value);}
            get { return rxData; }
        }

        public string Type
        {
            get { return type; }
        }
        
        public void AddView(IView view)
        {
            if (view != null)
            {
                this.views.Add(view);
            }
        }
 
        public void UpdateAllViews()
        {
            foreach (IView view in views)
            {
                if (view != null)
                {
                    view.UpdateD();
                }
            }
        }
        // delagate !!! 
        // draw fgv.
        
    }
}
